package cn.lingyangwl.agile.infra.weixin.mp.assembly;

import cn.lingyangwl.agile.infra.weixin.mp.model.entity.MpMessage;
import cn.lingyangwl.agile.infra.weixin.mp.model.entity.MpUser;
import cn.lingyangwl.agile.infra.weixin.mp.model.rqrs.MpMessageSendOutReq;
import cn.lingyangwl.agile.model.module.config.param.WxMpParam;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.builder.outxml.BaseBuilder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

/**
 * @author shenguangyang
 */
@Mapper(componentModel = "spring")
public interface MpMessageAssembly {
    default WxMpXmlOutMessage toWxMpXmlOutMessage(MpMessage message) {
        BaseBuilder<?, ? extends WxMpXmlOutMessage> builder;
        // 个性化字段
        switch (message.getType()) {
            case WxConsts.XmlMsgType.TEXT:
                builder = WxMpXmlOutMessage.TEXT().content(message.getContent());
                break;
            case WxConsts.XmlMsgType.IMAGE:
                builder = WxMpXmlOutMessage.IMAGE().mediaId(message.getMediaId());
                break;
            case WxConsts.XmlMsgType.VOICE:
                builder = WxMpXmlOutMessage.VOICE().mediaId(message.getMediaId());
                break;
            case WxConsts.XmlMsgType.VIDEO:
                builder = WxMpXmlOutMessage.VIDEO().mediaId(message.getMediaId())
                        .title(message.getTitle()).description(message.getDescription());
                break;
//            case WxConsts.XmlMsgType.NEWS:
////                builder = WxMpXmlOutMessage.NEWS().articles(convertList02(message.getArticles()));
//                break;
            case WxConsts.XmlMsgType.MUSIC:
                builder = WxMpXmlOutMessage.MUSIC().title(message.getTitle()).description(message.getDescription())
                        .musicUrl(message.getMusicUrl()).hqMusicUrl(message.getHqMusicUrl())
                        .thumbMediaId(message.getThumbMediaId());
                break;
            default:
                throw new IllegalArgumentException("不支持的消息类型：" + message.getType());
        }
        // 通用字段
//        builder.fromUser(account.getAccount());
        builder.toUser(message.getOpenid());
        return builder.build();
    }

    default MpMessage toMpMessage(WxMpXmlMessage wxMessage, MpUser user) {
        MpMessage message = toMpMessage(wxMessage);
        if (user != null) {
            message.setUserId(user.getId()).setOpenid(user.getOpenId());
        }
        return message;
    }

    @Mappings(value = {
            @Mapping(source = "msgType", target = "type"),
            @Mapping(target = "createdBy", ignore = true),
            @Mapping(target = "articles", ignore = true),
    })
    MpMessage toMpMessage(WxMpXmlMessage bean);

    default MpMessage toMpMessage(MpMessageSendOutReq rq, WxMpParam param, MpUser user) {
        // 构建消息
        MpMessage message = new MpMessage();
        message.setType(rq.getType());
        switch (rq.getType()) {
            case WxConsts.XmlMsgType.TEXT: // 1. 文本
                message.setContent(rq.getContent());
                break;
            case WxConsts.XmlMsgType.IMAGE: // 2. 图片
            case WxConsts.XmlMsgType.VOICE: // 3. 语音
                message.setMediaId(rq.getMediaId());
                break;
            case WxConsts.XmlMsgType.VIDEO: // 4. 视频
                message.setMediaId(rq.getMediaId())
                        .setTitle(rq.getTitle()).setDescription(rq.getDescription());
                break;
            case WxConsts.XmlMsgType.NEWS: // 5. 图文
                message.setArticles(rq.getArticles());
            case WxConsts.XmlMsgType.MUSIC: // 6. 音乐
                message.setTitle(rq.getTitle()).setDescription(rq.getDescription())
                        .setMusicUrl(rq.getMusicUrl()).setHqMusicUrl(rq.getHqMusicUrl())
                        .setThumbMediaId(rq.getThumbMediaId());
                break;
            default:
                throw new IllegalArgumentException("不支持的消息类型：" + message.getType());
        }

        // 其它字段
        if (param != null) {
            message.setMpAppId(param.getAppId());
        }
        if (user != null) {
            message.setUserId(user.getId()).setOpenid(user.getOpenId());
        }
        return message;
    }
}
